/** google global namespace for Google projects. */
var google = google || {};

/** devrel namespace for Google Developer Relations projects. */
google.devrel = google.devrel || {};

/** samples namespace for DevRel sample code. */
google.devrel.samples = google.devrel.samples || {};

/** hello namespace for this sample. */
google.devrel.samples.hello = google.devrel.samples.hello || {};

function writeCookie(name,value,days) {
    var date, expires;
    if (days) {
        date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        expires = "; expires=" + date.toGMTString();
            }else{
        expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var i, c, ca, nameEQ = name + "=";
    ca = document.cookie.split(';');
    for(i=0;i < ca.length;i++) {
        c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
        }
        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length,c.length);
        }
    }
    return '';
}


function getSearchParameters() {
    var prmstr = window.location.search.substr(1);
    return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
}

function transformToAssocArray( prmstr ) {
  var params = {};
  var prmarr = prmstr.split("&");
  for ( var i = 0; i < prmarr.length; i++) {
      var tmparr = prmarr[i].split("=");
      params[tmparr[0]] = tmparr[1];
  }
  return params;
}


/**
 * Prints a greeting to the greeting log.
 * param {Object} greeting Greeting to print.
 */
google.devrel.samples.hello.print = function(greeting,succ) {
  var element = document.createElement('div');
  element.classList.add('alert');
  if(succ) {
	  element.classList.add('alert-success');
  } else {
	  element.classList.add('alert-warning');
  }
  element.innerHTML = greeting;
  document.getElementById('outputLog').appendChild(element);
};

/**
 * Gets a numbered greeting via the API.
 * @param {string} id ID of the greeting.
 */
google.devrel.samples.hello.getGreeting = function() {
  gapi.client.smuapi.postStatus({
	  'message': document.getElementById('message').value,
	  'to': document.getElementById('to').value,
	  'tokenFb': document.getElementById('tokenFb').value,
	  'tokenTw1': document.getElementById('tokenTw1').value,
	  'tokenTw2': document.getElementById('tokenTw2').value}).execute(
      function(resp) {
        if (!resp.code) {
          google.devrel.samples.hello.print("<strong>message: </strong>" + document.getElementById('message').value + "has been sent to " + document.getElementById('to').value, true);
        } else {
            google.devrel.samples.hello.print("<strong>Error: </strong>" + resp.message, false);
        }
      });
};





/**
 * Enables the button callbacks in the UI.
 */
google.devrel.samples.hello.enableButtons = function() {
  document.getElementById('getGreeting').onclick = function() {
    google.devrel.samples.hello.getGreeting();
  };
};

/**
 * Initializes the application.
 * @param {string} apiRoot Root of the API's path.
 */
google.devrel.samples.hello.init = function(apiRoot) {
  var callback = function() {
     google.devrel.samples.hello.enableButtons();
     var params = getSearchParameters(); 
     document.getElementById('to').value = "Twitter";
     if(params.tokenFb != undefined) {
    	 writeCookie("tokenFb", params.tokenFb);
         document.getElementById('tokenFb').value = params.tokenFb;
     }else if(readCookie("tokenFb") != undefined) {
         document.getElementById('tokenFb').value = readCookie("tokenFb");
     }
     if(params.tokenTw1 != undefined) {
    	 writeCookie("tokenTw1", params.tokenTw1);
    	 document.getElementById('tokenTw1').value = params.tokenTw1;
     }else if(readCookie("tokenTw1") != undefined) {
         document.getElementById('tokenTw1').value = readCookie("tokenTw1");
     }
     if(params.tokenTw2 != undefined) {
    	 writeCookie("tokenTw2", params.tokenTw2);
         document.getElementById('tokenTw2').value = params.tokenTw2;
     }else if(readCookie("tokenTw2") != undefined) {
         document.getElementById('tokenTw2').value = readCookie("tokenTw2");
     }
  };

  gapi.client.load('smuapi', '1.5', callback, apiRoot);
};
